package beans;

import java.util.Properties;

import javax.ejb.Stateless;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import dto.DTOUser;

/**
 * Stateless Local EJB Bean which sends an email notification to a newly created
 * user's email address.
 * 
 * @author Mihai
 */
@Stateless
public class SendEmailLocalBean {

	private final String from = "Mihai.Cioban@msg-systems.com";
	private final String userName = "Mihai.Cioban@msg-systems.com";
	private final String password = "wowwow";
	private final String host = "msgc13i.msg.de";

	private String subject;
	private String content;

	/**
	 * Default constructor.
	 */
	public SendEmailLocalBean() {
	}

	/**
	 * Sends a notification email to a specified user, informing them of their
	 * new account on MSGWiki.
	 * 
	 * @param user
	 *            The user to which the notification email is being sent.
	 */
	public void sendNotificationEmail(DTOUser user, String language) {
		
		setTextByLanguage(user, language);

		Properties props = System.getProperties();
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");

		Authenticator auth = new SMTPAuthenticator();

		Session session = Session.getInstance(props, auth);

		Message msg = new MimeMessage(session);

		try {
			msg.setFrom(new InternetAddress(from));
			msg.setRecipient(RecipientType.TO,
					new InternetAddress(user.getEmail()));
			msg.setSubject(subject);

			MimeBodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(content);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			msg.setContent(multipart);

			Transport.send(msg);

		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}

	private void setTextByLanguage(DTOUser user, String language) {

		if (language.equals("de")) {
			content = "Willkommen " + user.getName() + ", um Wiki .msg. "
					+ "\n\nDein temporäres Passwort ist: " + user.getName() + "."
					+ "\nBitte ändern Sie es auf deinem ersten Login."
					+ "\n\nWir wünschen dir alles Gute!" + "\n\nDas .msg Wiki Team";
			subject = "Willkommen um Wiki .msg!";
		}
 
		if (language.equals("en")) {
			content = "Welcome " + user.getName() + ", to Wiki .msg. "
					+ "\n\nYour temporary password is: " + user.getName() + "."
					+ "\nPlease take the time and change it on your first login."
					+ "\n\nWish you all the best!" + "\n\nThe .msg Wiki Team";
			subject = "Welcome to Wiki .msg!";
		}

		if (language.equals("ro")) {
			content = "Bine ai venit, " + user.getName() + ", la Wiki .msg. "
					+ "\n\nParola ta temporară este: " + user.getName() + "."
					+ "\nTe rugăm să ți-o schimbi la prima ta logare."
					+ "\n\nÎți dorim toate cele bune!" + "\n\nEchipa Wiki .msg";
			subject = "Bine ai venit la .msg Wiki!";
		}

		if (language.equals("hu")) {
			content = "Isten hozott, " + user.getName() + ", az .msg Wiki-nél "
					+ "\n\nAz ideiglenes jelszavad: " + user.getName() + "."
					+ "\nMegkérünk, hogy az első bejelentkezésedkor változtasd meg."
					+ "\n\nMinden jót kíván" + "\n\naz .msg Wiki csapat!";
			subject = "Isten hozott az .msg Wiki-nél";
		}

	}

	/**
	 * Inner Class that makes the authentication on the mail account.
	 * 
	 * @author Mihai
	 * 
	 */
	private class SMTPAuthenticator extends javax.mail.Authenticator {

		@Override
		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(userName, password);
		}
	}

}
